

package com.mq;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.rocketmq.client.apis.ClientConfiguration;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.ClientServiceProvider;
import org.apache.rocketmq.client.apis.SessionCredentialsProvider;
import org.apache.rocketmq.client.apis.StaticSessionCredentialsProvider;
import org.apache.rocketmq.client.apis.message.Message;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.apache.rocketmq.client.apis.producer.SendReceipt;
import org.apache.rocketmq.shaded.org.slf4j.Logger;
import org.apache.rocketmq.shaded.org.slf4j.LoggerFactory;

public class ProducerNormalMessageExample {
    private static final Logger log = LoggerFactory.getLogger(ProducerNormalMessageExample.class);

    private ProducerNormalMessageExample() {
    }

    public static void main(String[] args) throws ClientException, IOException {
        final ClientServiceProvider provider = ClientServiceProvider.loadService();

        // 凭证提供程序是客户端配置的可选项。
        String accessKey = "LTAIjStBLoK6oZ9c";
        String secretKey = "67X2RSFZ0l703eQGpNpRMofeliDjha";
        SessionCredentialsProvider sessionCredentialsProvider =
                new StaticSessionCredentialsProvider(accessKey, secretKey);

        String endpoints = "http://onsaddr.mq-internet-access.mq-internet.aliyuncs.com:80";
        ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
                .setEndpoints(endpoints)
                .setCredentialProvider(sessionCredentialsProvider)
                .build();
        String topic = "dache-tpms";
        // 在大多数情况下，您不需要创建太多生产者，推荐使用单例模式。
        final Producer producer = provider.newProducerBuilder()
                .setClientConfiguration(clientConfiguration)
                // 设置主题名称（可选但推荐）。它使生产者能够在消息发布前预取主题路由。
                .setTopics(topic)
                // 如果生产者未初始化，可能会抛出 {@link ClientException}。
                .build();
        // 定义您的消息正文。
        String msg ="{\n" +
                "    \"comments\": \"(测试)轮位2轴左,胎温84(胎温≥84℃)告知司机需立即靠边停车检查轮胎\",\n" +
                "    \"slightHighPressure\": \"12.5\",\n" +
                "    \"latitude\": 38.583873,\n" +
                "    \"isHighPressure\": 0,\n" +
                "    \"pressure\": 10.5,\n" +
                "    \"deviceId\": \"869786066442181\",\n" +
                "    \"speed\": 76.0,\n" +
                "    \"sensorId\": \"81243FE5\",\n" +
                "    \"isLowPressure\": 0,\n" +
                "    \"isHighTemperature\": 1,\n" +
                "    \"licensePlate\": \"皖CDE02挂\",\n" +
                "    \"receiptTime\": \"2024-06-18 15:52:22\",\n" +
                "    \"sensorVoltage\": 2.2000000476837158203125,\n" +
                "    \"daqTime\": \"2024-06-18 15:52:20\",\n" +
                "    \"slightLowPressure\": \"7.0\",\n" +
                "    \"temperature\": 84,\n" +
                "    \"alarmSubType\": 9,\n" +
                "    \"seriousHighPressure\": \"13.0\",\n" +
                "    \"seriousLowPressure\": \"5.0\",\n" +
                "    \"location\": 5,\n" +
                "    \"slightHighTemperature\": \"79\",\n" +
                "    \"seriousHighTemperature\": \"84\",\n" +
                "    \"longitude\": 115.106818\n" +
                "}\n";
        byte[] body = msg.getBytes(StandardCharsets.UTF_8);
        String tag = "tyrePressureWarningTehst";
        final Message message = provider.newMessageBuilder()
                // 为当前消息设置主题。
                .setTopic(topic)
                // 消息的次级分类器，除了主题之外。
                .setTag(tag)
                // 消息的键，除了消息 ID 之外的另一种标记消息的方式。
                .setKeys("yourMessageKey-1c151062f96e")
                .setBody(body)
                .build();
        try {
            final SendReceipt sendReceipt = producer.send(message);
            log.info("消息发送成功，messageId={}", sendReceipt.getMessageId());
        } catch (Throwable t) {
            log.error("消息发送失败", t);
        }
        // 当您不再需要它时关闭生产者。
        producer.close();
    }

}